package com.yuyu.commons.config.nacos;

import com.alibaba.nacos.api.config.listener.Listener;
import com.aliyun.openservices.shade.com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.Executor;

@Slf4j
public class NacosConfigListener implements Listener {

    @Resource
    private ThreadPoolTaskExecutor commonExecutor;

    @Override
    public Executor getExecutor() {
        return null;
    }

    @Override
    public void receiveConfigInfo(String config) {
        log.info("线程池配置动态更新{}",config);
        HashMap hashMap = JSONObject.parseObject(config, HashMap.class);
        int coreThread = Integer.parseInt(hashMap.get("coreThread").toString());
        int maxThread = Integer.parseInt(hashMap.get("maxThread").toString());
        int queueCapacity = Integer.parseInt(hashMap.get("queueCapacity").toString());
        commonExecutor.setCorePoolSize(coreThread);
        commonExecutor.setMaxPoolSize(maxThread);
        commonExecutor.setQueueCapacity(queueCapacity);
        log.info("线程池动态更新完成");
    }
}
